package ext.weixin;

import helper.GlobalConfig;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 微信公众号工具类.
 */
public class WxMpHelper {

    private static final Logger logger = LoggerFactory.getLogger(WxMpHelper.class);
    private static WxMpConfigStorage wxMpConfigStorage = null;

    /**
     * 通过CorpApp得到微信配置.
     */
    public static WxMpConfigStorage getWxMpConfigStorage() {
        if (wxMpConfigStorage == null) {
            wxMpConfigStorage = initWxMpConfigStorage();
        }
        return wxMpConfigStorage;
    }

    private static WxMpConfigStorage initWxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(GlobalConfig.WEIXIN_APP_ID);     // 设置微信公众号的appid
        wxMpConfigStorage.setSecret(GlobalConfig.WEIXIN_APP_SECURET);    // 设置微信公众号的app corpSecret
        wxMpConfigStorage.setToken(GlobalConfig.WEIXIN_TOKEN);       // 设置微信公众号的token
        wxMpConfigStorage.setAesKey(GlobalConfig.WEIXIN_AES_KEY);      // 设置微信公众号的EncodingAESKey
        return wxMpConfigStorage;
    }


    /**
     * 通过ConfigStore得到微信服务接口.
     */
    public static WxMpService getWxMpService(WxMpConfigStorage wxMpConfigStorage) {
        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
        return wxMpService;
    }


}
